package com.meihuak.cloud_ordering.controller;

import com.meihuak.cloud_ordering.bean.Business;
import com.meihuak.cloud_ordering.bean.Canteen;
import com.meihuak.cloud_ordering.bean.Dish;
import com.meihuak.cloud_ordering.service.BusinessService;
import com.meihuak.cloud_ordering.service.CanteenService;
import com.meihuak.cloud_ordering.service.DishService;
import com.meihuak.cloud_ordering.service.ManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/canteen")
public class CanteenController {

    @Autowired
    CanteenService canteenService;
    @Autowired
    BusinessService businessService;
    @Autowired
    ManagerService managerService;
    @Autowired
    DishService dishService;

//废弃方法，新方法在BusinessController下的getAllInfo
//    @ResponseBody
//    @RequestMapping("/showall")
//    public Map<String,Object> showAllCanteen(){
//        Map<String,Object> resultMap = new HashMap<>();
//        List<Canteen> list = canteenService.list();
//        resultMap.put("msg","查询成功");
//        resultMap.put("info",list);
//        return resultMap;
//    }





    @ResponseBody
    @RequestMapping("/windows")
    public Map<String,Object> showAllBusinessById(@RequestParam(value = "id")Integer id){
        Map<String,Object> resultMap = new HashMap<>();
        List<Business> list = businessService.listByCanteenId(id);
        resultMap.put("msg","查询成功");
        resultMap.put("info",list);
        return resultMap;


    }




    @GetMapping("/infos")
    public ModelAndView getCanteen(){
        List<Canteen> canteens = canteenService.list();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("manager/canteen");
        modelAndView.addObject("canteens",canteens);
        return modelAndView;
    }

    @GetMapping("/canteeninfo")
    public ModelAndView getCanteeninfo(HttpSession session){
        Integer id = (int)session.getAttribute("id");
        ModelAndView modelAndView = new ModelAndView();
        if(managerService.getById(id)!=null){
            List<Canteen> canteens = canteenService.list();

            modelAndView.setViewName("manager/canteen1");
            modelAndView.addObject("canteens",canteens);
            return modelAndView;
        }
        modelAndView.setViewName("manager/error");
        return modelAndView;
    }




    @RequestMapping("/canteens")
    public String getCanteens(Model m){
        return "manager/canteen";

    }

    @RequestMapping("/businessinfo")
    public ModelAndView getCanteenInfoToHtml(@RequestParam(value = "id")Integer cid,
                                            HttpSession session){
        Integer uid = (int)session.getAttribute("id");
        ModelAndView modelAndView = new ModelAndView();
        if(managerService.getById(uid)!=null){
            List<Business> businesses = businessService.listByCanteenId(cid);
            modelAndView.setViewName("manager/window/canteeninfo");
            modelAndView.addObject("businesses",businesses);
            modelAndView.addObject("cid",cid);
            return modelAndView;
        }
        modelAndView.setViewName("manager/error");
        return modelAndView;
    }




}
